﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class ReadTextExample : MonoBehaviour {
    string kong = "";
    string NameContent = null;
    string WordContent = null;
	public Text NameText = null;
    public Text WordText = null;
    public Image TextImage;
    public GameObject choice;
    public GameObject Choices;
    public int counter = 0;
    public int tCounter;
    public List<GameObject> btns;
    int WritePeriod = 5;
    ReadText rt;
    bool OnceATime = true;


    public bool IsOver { get { return !isStart; } }
    public List<string> theChoices;


	void Start()
	{
        isCreated = false;
        tCounter = WritePeriod;
        //rt = gameObject.AddComponent<ReadText> ();
	}

	public void StartChat(string textPath)
	{
		if (isStart == false)
		{
			theChoices.Clear();
			isStart = true;
			WordText.text = kong;
			NameText.text = kong;
			rt = gameObject.AddComponent<ReadText> ();
			rt.TextToLines(textPath);
			//print("successful load");
			TextImage.gameObject.SetActive(true);
		}

	}
	public void StartChat(TextAsset textAsset)
	{
		if (isStart == false)
		{
			theChoices.Clear();
			isStart = true;
			WordText.text = kong;
			NameText.text = kong;
			rt = gameObject.AddComponent<ReadText> ();
			rt.TextToLines(textAsset);
			//print("successful load");
			TextImage.gameObject.SetActive(true);
		}

	}
    void WriteText()
    {
        tCounter--;
        if (counter <= WordContent.Length - 2)
        {
            if (tCounter < 0)
            {
                NameText.text = NameContent;
                WordText.text += WordContent[counter % WordContent.Length];
                counter++;
                tCounter = WritePeriod;
            }    
        }
    }

	//该标志可以通过触发器置true来开启对话
	bool isStart = false;
	void Update() {
		if (!isStart)
			return;
		if (rt.lines == null) {
			ChatEnd ();
			return;
		}

		if (rt.isThisLineChoice == false && MyInput.GetInputOfType (InputType.START_TALK))
		{
			if (counter < WordContent.Length - 2)
			{
				counter = WordContent.Length - 2;
				WordText.text = WordContent;
			}
			else
			{
				if (rt.setNextLine())
				{
					counter = 0;
					tCounter = WritePeriod;
					WordText.text = kong;
				}
				//如果返回false表示该对话已结束
				else
				{
					ChatEnd ();
				}
			}
		}

		GUIFunction ();
	}

    public bool isCreated;
	void GUIFunction(){
		if (rt.isThisLineChoice && !isCreated)
        {
            GameObject aChoie = null;
            foreach (ReadText.Choice_NextText ct in rt.cts)
            {
				aChoie = Instantiate(choice, Choices.transform.position, Choices.transform.rotation) as GameObject;
                aChoie.name = ct.choice;
                aChoie.transform.parent = Choices.transform;
                aChoie.transform.localScale = new Vector3(1, 1, 1);
                //aChoie.transform.position = aChoie.transform.parent.position;
                aChoie.GetComponentInChildren<Text>().text = ct.choice;
                aChoie.GetComponentInChildren<ChoiceFuc>().rt = rt;
                aChoie.GetComponentInChildren<ChoiceFuc>().ct = ct;
                btns.Add(aChoie.transform.gameObject);
            }
         
            NameText.text = kong;
            WordText.text = kong;
            isCreated = true;  //处理“一次”使用,在ChoiceFuc类里面初始化成false
		}
		//此处逻辑。。。
		//else if(!isCreated) {
		else if(!rt.isThisLineChoice) {
			NameContent = rt.person;
			WordContent =  rt.word;
            WriteText();
		}
	}
    
	void ChatEnd(){
		isStart = false;
		WordText.text = kong;
		NameText.text = kong;
		TextImage.gameObject.SetActive(false);
		Destroy (gameObject);
	}

	public static GameObject CreateTalkCanvas(string NewTextName, Vector3 pos, Quaternion rotation){
		GameObject newCanvas = GameObject.Instantiate(Resources.Load("ChatSystem/TalkUI"), pos, rotation) as GameObject; 
		newCanvas.GetComponent<ReadTextExample>().StartChat (NewTextName);
		return newCanvas;
	}
	public static GameObject CreateTalkCanvas(TextAsset newTextAsset, Vector3 pos, Quaternion rotation){
		GameObject newCanvas = GameObject.Instantiate(Resources.Load("ChatSystem/TalkUI"), pos, rotation) as GameObject; 
		newCanvas.GetComponent<ReadTextExample>().StartChat (newTextAsset);
		return newCanvas;
	}

}
